package com.qiyun.dinghuohui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.qiyun.dinghuohui.R;
import com.qiyun.dinghuohui.activity.DisplaysActivity;
import com.qiyun.dinghuohui.base.BaseFragment;
import com.qiyun.dinghuohui.common.ClientCookie;
import com.qiyun.dinghuohui.common.CommonDataSets;
import com.qiyun.dinghuohui.common.ImageUtils;
import com.qiyun.dinghuohui.pop.DisSearchChangePop;
import com.qiyun.dinghuohui.utils.CommonUtil;
import com.qiyun.dinghuohui.utils.Inject;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import static com.qiyun.dinghuohui.common.CommonDataSets.getDisplayNewById;

/**
 * Created by xlm on 2018\5\28 0028
 */

public class DisplayOrderFragment extends BaseFragment {

    @Inject(R.id.search_btn)
    private RelativeLayout mRelaSearch;//最右边search

    @Inject(R.id.search_icon)
    private RelativeLayout mRelaEditSearch;//输入框中search

    @Inject(R.id.search)
    private RelativeLayout mRelaEdit;//输入框部分

    @Inject(R.id.search_option)
    private TextView mTxtOption;//搜索条件
    @Inject(R.id.edit_search)
    private AppCompatEditText mEditSearch;//输入框
    @Inject(R.id.recyclerview)
    private RecyclerView mRecycle;
    @Inject(R.id.spuare)
    private RelativeLayout mRelaSquare;
    @Inject(R.id.sort_choose)
    private LinearLayout mLinearSearchBtn;
    @Inject(R.id.search_option)
    private TextView mTxtSearch;
    private List<JSONObject> listGoods = new ArrayList<>();//商品信息
    private String serchFilter = "编号";

    private boolean isSquare = true;

    @Override
    protected void onSetContentView() {
        setContentView(R.layout.fragment_display);
    }

    @Override
    protected void initViews() {
        mRelaEdit.setVisibility(View.GONE);
        mRelaSearch.setVisibility(View.VISIBLE);
        mTxtOption.setText("编号");
        mEditSearch.setHint("按编号搜索");
        serchFilter = "编号";
        isSquare = true;
        mRelaSquare.setSelected(false);
        mLinearSearchBtn.setSelected(false);
    }

    @Override
    protected void onSetListener() {
        mLinearSearchBtn.setOnClickListener(getCurrentFragment());
        mRelaSquare.setOnClickListener(getCurrentFragment());
        mRelaSearch.setOnClickListener(getCurrentFragment());
        mRelaEditSearch.setOnClickListener(getCurrentFragment());
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.search_btn:
                mRelaEdit.setVisibility(View.VISIBLE);
                mRelaSearch.setVisibility(View.GONE);
                break;
            case R.id.search_icon:
                String text = mEditSearch.getText().toString();
                if (CommonUtil.isEmpty(text)) {
                    showToast("请输入" + serchFilter);
                } else {
                    //按照选择搜索
                    displayAdapter.setNewData(getDisplayNewById(text.toUpperCase()));
                    if (getDisplayNewById(text.toUpperCase()).size() == 0) {
                        showToast("没有内容");
                    }
                    mEditSearch.setText("");
                    mRelaEdit.setVisibility(View.GONE);
                    mRelaSearch.setVisibility(View.VISIBLE);
                }
                break;
            case R.id.spuare:
                if (isSquare) {
                    isSquare = false;
                    mRelaSquare.setSelected(false);
                    setDisplayAdapter(false);
                } else {
                    isSquare = true;
                    mRelaSquare.setSelected(true);
                    setDisplayAdapter(true);
                }
                break;
            case R.id.sort_choose:
                showSearchPop();
                if (mSearchPop.getPopupWindow().isShowing()) {
                    mLinearSearchBtn.setSelected(true);
                }
                break;
        }
    }

    private DisSearchChangePop mSearchPop;

    private void showSearchPop() {
        if (mSearchPop == null) {
            mSearchPop = new DisSearchChangePop(getActivity());
            mSearchPop.setFocusable(true);
            mSearchPop.setListener(new DisSearchChangePop.Listener() {
                @Override
                public void onSelected(String filter) {
                    serchFilter = filter;
                    mEditSearch.setText("");
                    mEditSearch.setHint("按" + serchFilter + "搜索");
                    mTxtSearch.setText(serchFilter);
                }
            });
            mSearchPop.getPopupWindow().setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    hidePop();
                }
            });
        }
        mSearchPop.showAsDropDown(mRelaEdit);
    }

    private void hidePop() {
        mLinearSearchBtn.setSelected(false);
    }

    @Override
    protected void afterOnCreate(Bundle savedInstanceState) {
        listGoods = CommonDataSets.ds_display_new;
        setDisplayAdapter(true);
    }

    private DisplayAdapter displayAdapter;

    private void setDisplayAdapter(boolean square) {
        mRecycle.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        displayAdapter = new DisplayAdapter(square ? R.layout.item_display : R.layout.item_display_list, listGoods);
        mRecycle.setAdapter(displayAdapter);
        displayAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                DisplaysActivity.startActivity(getActivity(), listGoods.get(position));
            }
        });
    }

    private class DisplayAdapter extends BaseQuickAdapter<JSONObject, BaseViewHolder> {

        private int layoutId;

        public DisplayAdapter(int layoutResId, @Nullable List<JSONObject> data) {
            super(layoutResId, data);
            this.layoutId = layoutResId;
        }

        @Override
        protected void convert(BaseViewHolder helper, JSONObject item) {
            String code = item.optString("display");
            switch (layoutId) {
                case R.layout.item_display:
                    ImageUtils.showImage(getActivity(), (ImageView) helper.getView(R.id.item_display_image),
                            ClientCookie.getServerUrl() + "/goods/display/" + code + ".jpg");
                    helper.setText(R.id.item_display_code, code);
                    helper.setText(R.id.item_display_memo, item.optString("memo"));
                    break;
                case R.layout.item_display_list:
                    ImageUtils.showImage(getActivity(), (ImageView) helper.getView(R.id.item_display_list_image),
                            ClientCookie.getServerUrl() + "/goods/display/" + code + ".jpg");
                    helper.setText(R.id.item_display_list_code, code);
                    helper.setText(R.id.item_display_list_memo, item.optString("memo"));
                    break;
            }
        }
    }
}
